package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDTO;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送手机验证码
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session) {
        String phone = user.getPhone();

        if(StringUtils.isEmpty(phone)) {
            return R.error("手机短信验证码发送失败");
        }

        // 生成验证码
        String validateCode = ValidateCodeUtils.generateValidateCode4String(4);
        // 保存验证码
//        session.setAttribute(phone, validateCode);
        // 保存验证码到redis，并设置过期时间5分钟
        redisTemplate.opsForValue().set(phone, validateCode,5, TimeUnit.MINUTES);

        // 发送验证码短信
//        SMSUtils

        // 打印，方便测试
        log.info("验证码：{}", validateCode);
        return R.success("手机短信验证码发送成功");
    }


    @PostMapping("/login")
    public R<User> login(@RequestBody Map<String, String> paramMap, HttpSession httpSession) {
        log.info("接收到参数：{}", paramMap);

        String phhone = paramMap.get("phone");
        String code = paramMap.get("code");

        if(StringUtils.isEmpty(phhone) || StringUtils.isEmpty(code)) {
            return R.error("手机号或验证码错误！");
        }

//        String sessionCode = (String) httpSession.getAttribute(phhone);
        String cacheCode = (String) redisTemplate.opsForValue().get(phhone);
        if(code.equals(cacheCode)) {

            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getPhone, phhone);
            User user = userService.getOne(wrapper);
            if(user == null) {
                user = new User();
                user.setPhone(phhone);
                user.setStatus(1);
                userService.save(user);
            }
            httpSession.setAttribute("user", user.getId());
            // 登录成功删除验证码
            redisTemplate.delete(phhone);
            return R.success(user);
        }
        return R.error("登录失败");
    }

}
